package model;

public class PAppGrid {
	private PAppRect[][] squareGrid;
	private int canvasHeight = 571;
	private int canvasWidth = 600;
	private int sideSize;
	
	public PAppGrid(int sideSquares) {
		sideSize=sideSquares;
		initializeGrid();
	}
	
	public PAppRect[][] getSquareGrid() {
		return squareGrid;
	}

	public int getCanvasHeight() {
		return canvasHeight;
	}

	public void setCanvasHeight(int canvasHeight) {
		this.canvasHeight = canvasHeight;
	}

	public int getCanvasWidth() {
		return canvasWidth;
	}

	public void setCanvasWidth(int canvasWidth) {
		this.canvasWidth = canvasWidth;
	}
	
	public PAppRect getElement(int index){
		return squareGrid[index/sideSize][index%sideSize];
	}
	
	public int getPlayerIndex(){
		for(int i=0;i<sideSize*sideSize;i++){
			if(getElement(i).getActiveOption().equals(PathOptions.PLAYER)){
				return i;
			}
		}			
		return -1;
	}
	
	private void initializeGrid(){
		squareGrid = new PAppRect[sideSize][sideSize];
		int gridPosX = 0, gridPosY = 0, sideLenghtX = canvasWidth/sideSize, sideLenghtY = canvasHeight/sideSize;		
		for(int i=0;i<sideSize;i++){
			for(int j=0;j<sideSize;j++){ 
				squareGrid[i][j] = new PAppRect(gridPosX, gridPosY, sideLenghtX, sideLenghtY);
				gridPosX+=sideLenghtX;
				if(gridPosX>=canvasWidth){
					gridPosX=0;
					gridPosY+=sideLenghtY;
				}
			}
		}
	}

	public int getSideSize() {
		return sideSize;
	}
}
